﻿Public MustInherit Class Hareket : Implements IComparable

	Public Kod As Integer
	Public Tarih As Date
	Public SaatBakiyesi As Decimal

	Public Islem As String
	Public Tarih1 As Date
	Public Tarih2 As Date
	Public Current As IzinBilgisi
	Public Draft As Boolean
	Public EvrakNo As String

	Public Function Calculate(ByVal Previous As IzinBilgisi, ByVal Cins As String) As IzinBilgisi
		Current = Previous
		Current.Draft = Draft
		Current.GunHesabi = False
		Current.Yil = Tarih.Year
		Current.EvrakNo = EvrakNo
		Process(Cins)
		Current.Kod = Kod
		Return Current
	End Function

	MustOverride Sub Process(ByVal Cins As String)

	ReadOnly Property Gunluk As Boolean
		Get
			Return Not TypeOf Me Is HareketSaatlikIzin
		End Get
	End Property

	Function IPGMAyIzin(Ay As Integer) As Integer
		Return Choose(Ay, 0, 1, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24)
	End Function

	Sub GunYurut(Optional ByVal Ekstra As Integer = 0)
		If Current.Statu = "IP" Then
			Dim Tarih = Me.Tarih.AddDays(-1)
			Current.Normal.Kazanilan = 0
			If Current.YeniKayit Then
				Current.Normal.Kazanilan = (Tarih.AddDays(1).Year - Current.BaslamaTarihi.Year) * 22 + IPGMAyIzin(Tarih.AddDays(1).Month) - IPGMAyIzin(Current.BaslamaTarihi.Month)
				If Current.BaslamaTarihi.Day >= 15 Then Current.Normal.Kazanilan -= 1
			Else
				Current.Normal.Kazanilan = (Tarih.Year - Current.BaslamaTarihi.Year) * 22 + IPGMAyIzin(Tarih.AddDays(1).Month + If(Tarih.ToString("MMdd") = "1231", 12, 0))
			End If
			Current.Normal.Toplam = Current.Normal.Kazanilan + Current.Normal.Varolan
			Current.Normal.Kalan = Current.Normal.Toplam - Current.Normal.YilToplami
			Current.GunHesabi = True
		Else
			Dim Tarih = Me.Tarih.AddDays(-1)
			'Geçen süreyi hesapla
			If Current.Tarih.Year > 1000 And Current.Statu > "" Then
				Current.GecenSure.Ay = 0
				Current.GecenSure.Gun = Ekstra
				If Current.Tarih.YMD.Substring(0, 6) = Tarih.YMD.Substring(0, 6) Then
					If Current.Tarih = Current.Tarih.FirstDayOfMonth And Tarih = Tarih.LastDayOfMonth Then
						Current.GecenSure.Ay = 1
					Else
						Current.GecenSure.Gun += DateDiff(DateInterval.DayOfYear, Current.Tarih, Tarih) + 1
					End If
				Else
					If Current.Tarih.Day > 1 Then Current.GecenSure.Gun += DateDiff(DateInterval.DayOfYear, Current.Tarih, Current.Tarih.LastDayOfMonth) + 1
					If Tarih < Tarih.LastDayOfMonth Then Current.GecenSure.Gun += Tarih.Day
					Current.GecenSure.Ay = DateDiff(DateInterval.Month, Current.Tarih.AddDays(-1).FirstDayOfMonth.AddMonths(1), Tarih.AddDays(1).FirstDayOfMonth)
					Current.GecenSure.Ay += Int(Current.GecenSure.Gun / 30)
					Current.GecenSure.Gun = Current.GecenSure.Gun Mod 30
				End If
				Current.KazTarihAraligi = Current.Tarih.DMY2 & "-" & Tarih.DMY2
				'Hakedilen izni hesapla
				Current.GunBakiyesi = Current.Hakedilen.Gun
				Current.Hakedilen.Gun += Current.GecenSure.Gun
				Current.Hakedilen.Ay = Current.GecenSure.Ay + Int(Current.Hakedilen.Gun / 30)
				Current.Hakedilen.Gun = Current.Hakedilen.Gun Mod 30
				'Toplam izni hesapla
				Current.Normal.Varolan = Current.Normal.Kalan
				Current.GecerliStatu = Current.Statu
				Current.Normal.Kazanilan = Current.Hakedilen.Ay * StatuListesi(Current.Statu).Izin
				Current.Normal.Toplam = Current.Normal.Kalan + Current.Normal.Kazanilan
				Current.Normal.Kalan = Current.Normal.Toplam
			End If
			Current.GunHesabi = Current.GecenSure.Gun > 0 Or Current.GecenSure.Ay > 0
		End If
	End Sub

	Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo
		Dim h As Hareket = obj
		If Tarih < h.Tarih Then
			Return -1
		ElseIf Tarih > h.Tarih Then
			Return 1
		ElseIf TypeOf Me Is HareketIlk Or TypeOf Me Is HareketYilbasi Then
			Return -1
		ElseIf TypeOf obj Is HareketIlk Or TypeOf obj Is HareketYilbasi Then
			Return 1
		ElseIf TypeOf Me Is HareketBugun Then
			Return 1
		ElseIf TypeOf obj Is HareketBugun Then
			Return -1
		Else
			Return 0
		End If
	End Function

End Class

Public Class HareketIlk : Inherits Hareket

	Public IlkStatu As String
	Public IlkIzin As Decimal
	Public IlkBakiye As Integer

	Public Overrides Sub Process(ByVal Cins As String)
		Current.Tur = "ilk"
		Current.Islem = "İLK KAYIT"
		Current.Statu = IlkStatu
		Current.Tarih = Tarih
		Current.Normal.Kalan = IlkIzin
		Current.Normal.YilToplami = 0
		Current.Hakedilen.Gun = IlkBakiye
		Current.Hastalik.Kalan = StatuListesi(Current.Statu).YillikHastalik
		Current.Mazaret.Kalan = StatuListesi(Current.Statu).YillikMazaret(Cins)
		If Current.Statu = "IP" Then
			Current.Normal.Varolan = Current.Normal.Kalan
		End If
		If Current.Tarih.ToString("MMdd") <> "0101" Then
			Current.BaslamaTarihi = Current.Tarih
			Current.YeniKayit = True
		Else
			Current.BaslamaTarihi = Tarih
		End If
	End Sub

End Class

Public Class HareketYillikIzin : Inherits Hareket

	Public Overrides Sub Process(ByVal Cins As String)
		Current.Tur = "yillik"
		Current.Islem = "YILLIK İZİN"
		Current.Tarih1 = Tarih1
		Current.Tarih2 = Tarih2
		Current.TarihAraligi = Tarih1.DMY2 & "-" & Tarih2.DMY2
		Current.Normal.Kullanilan = GunHesapla(Tarih1, Tarih2)
		GunYurut(GunHesapla(Tarih1, Tarih2, True))
		Current.Normal.YilToplami += Current.Normal.Kullanilan
		Current.Normal.Kalan -= Current.Normal.Kullanilan
		Select Case Current.GecerliStatu
			'Case "IS" : Current.Tarih = Tarih1
			Case Else : Current.Tarih = Tarih2.AddDays(1)
		End Select

	End Sub

End Class

Public Class HareketStatu : Inherits Hareket

	Public YeniStatu As String

	Public Overrides Sub Process(ByVal Cins As String)
		Current.Tur = "statu"
		Current.Islem = "STATÜ DĞŞK."
		GunYurut()
		Current.Statu = YeniStatu
		Current.Tarih = Tarih
	End Sub

End Class

Public Class HareketYilsonu : Inherits Hareket

	Public Overrides Sub Process(ByVal Cins As String)
		Current.Tur = "yilsonu"
		Current.Islem = "YIL SONU"
		Tarih = Tarih.AddDays(1)
		GunYurut()
	End Sub

End Class

Public Class HareketBugun : Inherits Hareket

	Public Overrides Sub Process(ByVal Cins As String)
		Current.Tur = "bugun"
		Current.Islem = "BUGÜN"
		Tarih = Tarih.AddDays(1)
		GunYurut()
		Current.Tarih = Tarih
	End Sub

End Class

Public Class HareketYilbasi : Inherits Hareket

	Public Overrides Sub Process(ByVal Cins As String)
		Current.Tur = "yilbasi"
		Current.Islem = "YIL AÇILIŞI"
		Dim YillikMax As Integer = If(Current.Statu = "A", 60, 84)
		'Dim YilAyarlamasi As Integer = 14 - Current.Normal.YilToplami
		'If YilAyarlamasi > 0 Then Current.Normal.Kalan -= YilAyarlamasi
		If Current.Normal.Kalan >= YillikMax Then
			Current.Normal.Kalan = YillikMax
			Current.Hakedilen.Gun = 0
		End If
		Current.Normal.YilToplami = 0
		Current.Hastalik.YilToplami = 0
		Current.Mazaret.YilToplami = 0
		If Current.Statu > "" Then
			Current.Hastalik.Kalan = StatuListesi(Current.Statu).YillikHastalik
			Current.Mazaret.Kalan = StatuListesi(Current.Statu).YillikMazaret(Cins)
		End If
		Current.Tarih = Tarih
	End Sub

End Class

Public Class HareketHastalikIzni : Inherits Hareket

	Public Overrides Sub Process(ByVal Cins As String)
		Current.Tur = "hastalik"
		Current.Islem = "HASTALIK İZNİ"
		Current.TarihAraligi = Tarih1.DMY2 & "-" & Tarih2.DMY2
		Current.Hastalik.Kullanilan = DateDiff(DateInterval.DayOfYear, Tarih1, Tarih2) + 1
		If Current.Hastalik.Kullanilan > Current.Hastalik.Kalan Then
			Tarih = Tarih1.AddDays(Math.Max(0, Current.Hastalik.Kalan))
			GunYurut()
			Current.Tarih = Tarih2.AddDays(1)
		End If
		Current.Hastalik.YilToplami += Current.Hastalik.Kullanilan
		Current.Hastalik.Kalan -= Current.Hastalik.Kullanilan
		'If Current.Hastalik.Kalan < 0 Then Current.Hastalik.Kalan = 0
	End Sub

End Class

Public Class HareketMazaretIzni : Inherits Hareket

	Public Overrides Sub Process(ByVal Cins As String)
		Current.Tur = "mazaret"
		Current.Islem = "MAZARET İZNİ"
		Current.TarihAraligi = Tarih1.DMY2 & "-" & Tarih2.DMY2
		Current.Mazaret.Kullanilan = DateDiff(DateInterval.DayOfYear, Tarih1, Tarih2) + 1
		Current.Mazaret.YilToplami += Current.Mazaret.Kullanilan
		Current.Mazaret.Kalan -= Current.Mazaret.Kullanilan
	End Sub

End Class

Public Class HareketOzelMazaretIzni : Inherits Hareket

	Public Overrides Sub Process(ByVal Cins As String)
		Current.Tur = "ozelmazaret"
		Current.Islem = "ÖZEL MAZ. İZNİ"
		Current.TarihAraligi = Tarih1.DMY2 & "-" & Tarih2.DMY2
		Current.Mazaret.Kullanilan = DateDiff(DateInterval.DayOfYear, Tarih1, Tarih2) + 1
	End Sub

End Class

Public Class HareketSaatlikToplamIzin : Inherits Hareket

	Public Gun As Integer

	Public Overrides Sub Process(ByVal Cins As String)
		Current.Tur = "saattoplam"
		Current.Islem = "TOPLAM SAATLİK"
		Current.TarihAraligi = Tarih.DMY2 & "-SAATLİK"
		Current.Normal.Kullanilan = Gun
		Current.Normal.YilToplami += Gun
		Current.Normal.Kalan -= Gun
	End Sub

End Class

Public Class HareketSaatlikIzin : Inherits Hareket

	Public Saat1, Saat2 As String

	Public Overrides Sub Process(ByVal Cins As String)
		Current.Tur = "saatlik"
		Current.Islem = "SAATLİK"
		Current.Saatlik.Kullanilan = SaatHesapla(Saat1, Saat2)
		Current.Saatlik.Toplam += Current.Saatlik.Kullanilan
	End Sub

End Class

Public Structure IzinBilgisi
	Public Kod As Long
	Public Yil As Integer
	Public KazTarihAraligi As String
	Public Islem As String
	Public GunHesabi As Boolean
	Public Tur As String
	Public Tarih As Date
	Public TarihAraligi As String
	Public GecenSure As Sure
	Public Hakedilen As Sure
	Public GecerliStatu As String
	Public Statu As String
	Public Saatlik As Izin
	Public Normal As Izin
	Public Mazaret As Izin
	Public Hastalik As Izin
	Public Draft As Boolean
	Public EvrakNo As String
	Public BaslamaTarihi As Date
	Public YeniKayit As Boolean
	Public GunBakiyesi As Integer
	Public Tarih1 As Date
	Public Tarih2 As Date
End Structure

Public Structure TalepBilgisi
	Public Kod As Long
	Public Personel As Integer
	Public Tarih1 As Date
	Public Tarih2 As Date
	Public Durum As String
End Structure


Public Structure Izin
	Public Varolan As Decimal
	Public Kazanilan As Decimal
	Public Toplam As Decimal
	Public Kullanilan As Decimal
	Public Kalan As Decimal
	Public YilToplami As Decimal
End Structure

Public Structure Sure
	Public Ay As Integer
	Public Gun As Integer
End Structure